Converting QBASIC to Liberty BASIC

by Gordon Sweet Gordon@gsweet.fsnet.co.uk


Home

Newsletter Index - Terra

Sprite Byte - Watson

Filtered Numeric Input - Bromley

Changing the cursor - Sweet

QBASIC to Liberty BASIC - Sweet

Contact Manager - Lewis

LameCard - Nally

Submission Guildlines

Newsletter Help

Index

Many people like me started using [Liberty BASIC] after some experience at using Qbasic and or the compiler version QuickBasic. To help understand the differences between QB and Liberty BASIC, and how to upgrade programs in Qbasic, I offer a few tips here in the form of an LB program. The program offers two routines. The first uses a Graphics Window and lists commonly used commands for either language. The second uses a Text Window and shows how to retrieve text from a texteditor and manipulate that text in the Text Window.

Remember unlike QB, LB cannot display text in more than one colour using a Text Window, or invoke a coloured background. This is why the graphics window was more suitable for displaying the commands.

Complete beginners to LB might find it advisable to first try out a few routines using the opening Mainwindow of LB before invoking any other window. The Mainwindow is limited to Text only, but Liberty BASIC Version 4 does support a LOCATE x,y and a PRINT TAB(COLUMN); similar to QB.

The Program below can be pasted into LB. It is compatible with Version 3.03 as well as Version 4.x

Gordon Sweet -- Gordon@gsweet.fsnet.co.uk


nomainwin
    ux = 1 : uy = 1
    if DisplayWidth > 1000 then ux = 120 : uy = 90
[opts]
    UpperLeftX = ux : UpperLeftY = uy
    WindowWidth = 800 : WindowHeight = 580
    butnT$ = "  TEXT WINDOW "
    butnC$ = "SEE COMMANDS"
    button #q.b1, butnT$, [text], ll, 420, 40, 150, 40
    button #q.b2, butnC$, [commands], ll, 120, 40, 150, 40
    open "Qbasic to LB conversions" for graphics_nsb as #q
    #q "trapclose [quit]; down; font arial 18 bold; place 20 100"
    #q "\Examples of how to convert some Qbasic Commands to"
    #q "\Liberty Basic. Note the suggested Qbasic equivalents are on"
    #q "\the right in the program. Many in this program refer ONLY to"
    #q "\a GRAPHICS Window, and to certain Qbasic SCREEN modes."
    #q "\Others not shown are often identical to both languages."
    #q "\\CLICK BELOW TO    (1) SEE THE COMMANDS"
    #q "\\     - or -                     (2) RUN THE TEXT WINDOW EXAMPLE"
    #q "FLUSH"
'The FLUSH command causes the graphics text to be redrawn if the window
'is erased by moving, minimizing, covered by another window, etc.

    gosub [fillArrays]

    WAIT ' Close or activate TEXT or COMMAND button

[text]
'This routine shows how to select information from a texteditor and then
'paste that information in a text window.
'From the Liberty BASIC Help File: "Text windows always have a menubar
'that contains a ready-made File Menu and a ready-made Edit Menu."
    If textWin = 1 Then
    Close #t
    End If
    textWin = 1
'textWin = 1: Flag to prevent user from trying to open a window that's already open
    TEXTEDITOR #t.tb 20,20,0,0 'this is invisible
    OPEN "TEXT WINDOW EXAMPLES" for text_fs as #t
    #t "!trapclose [closeTextWin]"
    #t "!font arial 18 bold"
    #t.tb "Text Window LOCATE row, column Equivalent see [tbox] etc"
    colm = 20 : lin = 5
    GOSUB [tbox]
    ' NOTE THE USE OF      TEXTEDITOR #q.tb 20,20,0,0 'this is invisible
    ' and [tbox] to replace LOCATE row, column when using a TEXT Window.

    WAIT ' Await next button activation

[tbox]
    #t.tb "!SELECTALL";
    #t.tb "!CUT";
    #t "!SELECT ";colm;" ";lin
    #t "!PASTE";
    RETURN

[closeTextWin]
    Close #t
    textWin = 0
    WAIT

[commands]
'A graphics window is used to display this information so that text can
'be displayed in alternating colours, making the text more readable.
'From the Liberty BASIC Help File: "[Graphic windows] are not intended to contain
'controls, and some controls do not work properly when placed in a graphics window."
    If graphWin = 1 then
    Close #g
    End If
    graphWin = 1
'graphWin = 1: Flag to prevent user from trying to open a window that's already open
    OPEN "Liberty BASIC and QBASIC Equivalents" for graphics_fs as #g
    #g, "TRAPCLOSE [closeGraphWin]"
    #g, "CLS"
    #g, "Down"
    #g, "Place 20 20"
    #g, "Color Black"
    #g, "\";lb$(0);"\";qb$(0);"\"
    #g, "Color Red"
    #g, "\Text following a ~ is meant to be a remark or comment and is not part of the command\"
    #g, "Color DarkCyan"
    #g, "\Liberty BASIC Command";space$(68);"QBASIC Command"
    #g, "Color DarkGray"
    #g, "Line 20 108 780 108"
    #g, "Line 20 110 780 110"
    x = 20: y = 130
    For i = 1 to 25
    #g, "Color DarkBlue"
        If i/2 = INT(i/2) Then
        #g, "Color Blue"
        End If
    #g, "Place ";x;" ";y
    #g, "\";lb$(i)
    #g, "Place 450 ";y
    #g, "\";qb$(i)
    #g, "Color DarkGray"
    #g, "Line 20 ";y+4;" 780 ";y+4
    y = y + 24
    Next i
    #g, "FLUSH"
    WAIT

[closeGraphWin]
    Close #g
    graphWin = 0
    WAIT

[quit]
    CLOSE #q : END

'---------------------------------------------------------------------------------------------------------------
'The [pixel] code is a routine which finds the colour of the designated pixel.
'The r, g, b code determines the red, green and blue values based upon the
'pixcol value returned by the GetPixel API.
'The code is here to explain how to obtain the r, g, b values in Liberty BASIC.
'This code is not actually run during this program.
'---------------------------------------------------------------------------------------------------------------
[pixel]
    'IF REQUIRED TO DETECT AT MOUSE CURSOR after #q "when leftButtonDown [pixel]"
    xp=MouseX : yp=MouseY

    ' get pixel color at x & y
    calldll#user32,"GetDC", h as word, hdc as word
    calldll #gdi32,"GetPixel",_
        hdc as ulong,_
        xp as ulong,_
        yp as ulong,_
        pixcol as ulong

    CallDll #user32, "ReleaseDC",_
        hw as word,_ 'handle of graphic box
        hdc as word,_ 'dc from GetDC
        result as ushort 'returns nonzero if successful

    r = int(pixcol - b*256*256 - g*256)
    g = int((pixcol - b *256*256) / 256)
    b = int(pixcol / (256*256))
    RETURN

'Blue = int(pixelColor / (256*256))
'Green = int((pixelColor - Blue *256*256) / 256)
'Red = int(pixelColor - Blue*256*256 - Green*256)

'rgbcolor = redValue + (greenValue * 256) + (blueValue * 256*256)

'---------------------------------------------------------------------------------------------------------------
'These are the actual Liberty BASIC Commands that would be used in a program.
'The remarks to the right of each command, in gray following the apostrophe, is the
'suggested QB command.
'These commands are here for illustration examples and are not actually being executed
'in this program.
'---------------------------------------------------------------------------------------------------------------

    #q "place column row"                   ' LOCATE row, column
    #q "\";space$(n);                       ' PRINT TAB(column);
    playwave "any.wav"                      ' SOUND freq,dur and PLAY music$
' Study LADDER.BAS supplied with LB to generate Tones from the Console Speaker
    #q "font fontname size style"           ' similar to SCREEN n and WIDTH colums, rows
    #q "color red"                          ' COLOR 12    etc
    #q "backcolor green"                    ' COLOR 12, 10   (foreground, background)
    #q "fill green"                         ' PAINT (x, y), 10
    #q "goto x y"                           ' LINE -(x ,y)
    #q "place 100 100; goto 200 200"        ' LINE  (100, 100) - (200, 200)
    #q "place x y"                          ' PSET (x, y)
    #q "color green; set x y)"              ' PSET (x, y), 10
    x = 100 : y = 200 : gosub [pixel]   ' POINT (x%,y%)   gives color
    z = int(66.89+.5)   '( = 67)          z% = 66.89    (= 67)
    #q "getbmp bmpName x y width height"    ' GET (x1,y1)-(x2,y2),arrayname  GRAPHICS
    #q "drawbmp bmpname x y"                ' PUT (x,y),array                GRAPHICS
    bmpsave "bmpName", "filename.bmp"       ' BSAVE filename,offset,length   saves display
    LOADBMP "name", "filename.bmp"          ' BLOAD filename,offset          loads to memory
    #q "\";5 - int(5 / 2) * 2               ' PRINT 5 MOD 2
    #q "\";time$("seconds")                 ' PRINT TIMER    ( includes decimals )
    redim cust$(n)                          ' ERASE cust$
    ' open new4features.Isn and Joystick Suppert (LB V.4)   ' STICK(n%) and STRIG(n%)
    close #handle : end                     ' STOP or END
    #q "!LINES" : INPUT #q, lpos            ' y = CRSLIN    last line of print
    #q "!ORIGIN 1 1" ;                      ' LOCATE 1, 1
    #q USING("######.##", 123456.78)        ' PRINT USING "######.##";123456.78
'---------------------------------------------------------------------------------------------------------------
'In order to display the above commands as a string in the graphics window, some
'modifications needed to be made to accomodate the quotation marks and backslash.
'chr$(34) is the ascii which prints the quotation mark.  The | character is used instead
'of the \character.
'Array lb$() and qb$() hold the string commands for display purposes.  These arrays
'are filled during the execution of GOSUB [fillArrays]
[fillArrays]
    dim lb$(25)
    dim qb$(25)
    lb$(0) = "Use PRINT in the LB File menu to replace LLIST"
    qb$(0) = "likewise OPEN and SAVE instead of LOAD filename, SAVE filename and RUN"
    lb$(1) = "#q, ";chr$(34);"PLACE column row";chr$(34)
    qb$(1) = "LOCATE row, column"
    lb$(2) = "#q, ";chr$(34);"|SPACE$(n)";chr$(34);"   ~Substitute backslash key for |"
    qb$(2) = "PRINT TAB(column);"
    lb$(3) = "PLAYWAVE ";chr$(34);"any.wav"
    qb$(3) = "SOUND freq, dur and PLAY music$"
    lb$(4) = "#q, ";chr$(34);"FONT fontname size style";chr$(34)
    qb$(4) = "similar to SCREEN n and WIDTH columns, rows"
    lb$(5) = "#q, ";chr$(34);"COLOR red";chr$(34)
    qb$(5) = "COLOR 12   etc."
    lb$(6) = "#q, ";chr$(34);"BACKCOLOR green";chr$(34)
    qb$(6) = "COLOR 12, 10   (foreground, background)"
    lb$(7) = "#q, ";chr$(34);"FILL green";chr$(34)
    qb$(7) = "PAINT (x,y), 10"
    lb$(8) = "#q, ";chr$(34);"GOTO x y";chr$(34)
    qb$(8) = "LINE -(x,y)"
    lb$(9) = "#q, ";chr$(34);"PLACE 100 100; GOTO 200 200";chr$(34)
    qb$(9) = "LINE (100, 100) - (200, 200)"
    lb$(10) = "#q, ";chr$(34);"PLACE x y";chr$(34)
    qb$(10) = "PSET (x,y)"
    lb$(11) = "#q, ";chr$(34);"COLOR green; SET x y";chr$(34)
    qb$(11) = "PSET (x,y), 10"
    lb$(12) = "x=100: y=200: GOSUB [pixel]";chr$(34);"   ~[pixel] code included in program"
    qb$(12) = "POINT (x%,y%)"
    lb$(13) = "z=INT(66.89 + 0.5)     ~result = 67"
    qb$(13) = "z% = 66.89     ~result = 67"
    lb$(14) = "#q, ";chr$(34);"GETBMP bmpName x y width height";chr$(34)
    qb$(14) = "GET (x1,y1)-(x2,y2), arrayname GRAPHICS"
    lb$(15) = "#q, ";chr$(34);"DRAWBMP bmpname x y";chr$(34)
    qb$(15) = "PUT (x,y), array   GRAPHICS"
    lb$(16) = "BMPSAVE ";chr$(34);"bmpName";chr$(34);", ";chr$(34);"filename.bmp"
    qb$(16) = "BSAVE filename, offset, length   ~saves display"
    lb$(17) = "LOADBMP ";chr$(34);"name";chr$(34);", ";chr$(34);"filename.bmp"
    qb$(17) = "BLOAD filename,offset   ~loads to memory"
    lb$(18) = "#q, ";chr$(34);"|5 - INT(5/2) * 2";chr$(34);"   ~Substitute backslash key for |"
    qb$(18) = "PRINT 5 MOD 2"
    lb$(19) = "#q, ";chr$(34);"TIME$(";chr$(34);"seconds";chr$(34);")";chr$(34)
    qb$(19) = "PRINT TIMER   ~ (includes decimals)"
    lb$(20) = "REDIM cust$(n)"
    qb$(20) = "ERASE cust$"
    lb$(21) = "CLOSE #handle: END"
    qb$(21) = "STOP or END"
    lb$(22) = "~open new4features.Isn and Joystick Support (LBV.4)"
    qb$(22) = "STICK(n%) and STRIG(n%)"
    lb$(23) = "#q, ";chr$(34);"!LINES";chr$(34);": INPUT #q, lpos";chr$(34)
    qb$(23) = "y = CRSLIN   ~ last line of print"
    lb$(24) = "#q, ";chr$(34);"!ORIGIN 1 1";chr$(34)
    qb$(24) = "LOCATE 1, 1"
    lb$(25) = "#q, ";chr$(34);"USING(";chr$(34);"######.##";chr$(34);",123456.78)";chr$(34)
    qb$(25) = "PRINT USING ";chr$(34);"######.##";chr$(34);";123456.78"
RETURN
'---------------------------------------------------------------------------------------------------------------